/*
 * ControlPoint.cpp
 *
 *  Created on: 2010-9-8
 *      Author: wenjian
 */

#include "ControlPoint.h"
#include "../WorkLayer.h"
#include "../WorkPage.h"
#include <globes.h>

namespace S {

  unsigned long ControlPoint::ClassId = hashCode("S::ControlPoint");

  ControlPoint::ControlPoint(int index, Shape* shape):
    path(NULL), index(index), shape(shape), center(0,0)
    ,upDownCursor(LoadCursor(NULL,IDC_SIZENS))
    ,leftRightCursor(LoadCursor(NULL,IDC_SIZEWE))
    ,lowerLeftCursor(LoadCursor(NULL,IDC_SIZENWSE))
    ,lowerRightCursor(LoadCursor(NULL,IDC_SIZENESW))
    ,crossCursor(LoadCursor(NULL,IDC_CROSS))
    {

    pen = new Gdiplus::Pen(Gdiplus::Color(0, 0, 0));
    brush = new Gdiplus::SolidBrush(Gdiplus::Color(255, 255, 255, 255));

    initCursorStyle(index);
  }

//  ControlPoint::ControlPoint(int index, Shape* shape, real centerX, real centerY, real x, real y, real w, real h):
//       _w(0), _h(0), index(index), shape(shape) {
//    setCoordinate(x, y);
//    center.X = centerX;
//    center.Y = centerY;
//  }

  ControlPoint::~ControlPoint() {
    if (path)
      delete path;
    delete pen;
    delete brush;
  }

  void ControlPoint::setData(real centerX, real centerY, real x, real y, real w, real h){
    center.X = centerX;
    center.Y = centerY;
    setCoordinate(ceil(x), ceil(y));
    setWidth(ceil(w));
    setHeight(ceil(h));
    createPath();
  }

  void ControlPoint::createPath() {
    if (path) delete path;
    path = new Gdiplus::GraphicsPath();
    createBoundRect();
    getBoundRect(&shapeRect, -1.0f);
    if(index%2==1){
      path->AddRectangle(shapeRect);
      //path->AddRectangle(Gdiplus::RectF(x(), y(), width()-1, height()-1));
    }else{
      //path->AddEllipse(x(), y(), width()-1, height()-1);
      path->AddEllipse(shapeRect);
    }
//    destroyBoundRect();
//    createBoundRect();
    setModified(true);
  }

  void ControlPoint::draw(Gdiplus::Graphics* g) {
    g->FillPath(brush, path);
    g->DrawPath(pen, path);
    setModified(false);
    setRedraw(false);
  }

//  void ControlPoint::setMoveCursorStyle(){
////    WorkPage* page = (WorkPage*)(layer->getDrawPage());
////    WorkLayer* workLayer = page->getWorkLayer();
////    if(page->getWorkMode() == WorkPage::SelectMode){
////      //显示方向箭头
////      switch(index){
////        case 1:
////        case 5:
////          workLayer->setCursor(&upDownCursor);
////          break;
////        case 3:
////        case 7:
////          workLayer->setCursor(&leftRightCursor);
////          break;
////        case 0:
////        case 4:
////          workLayer->setCursor(&lowerLeftCursor);
////          break;
////        case 2:
////        case 6:
////          workLayer->setCursor(&lowerRightCursor);
////          break;
////      }
////    }
//  }

  /**
   * 初始化光标样式
   */
  void ControlPoint::initCursorStyle(int index){
        switch(index){
          case 1:
          case 5:
            setCursor(&upDownCursor);
            break;
          case 3:
          case 7:
            setCursor(&leftRightCursor);
            break;
          case 0:
          case 4:
            setCursor(&lowerLeftCursor);
            break;
          case 2:
          case 6:
            setCursor(&lowerRightCursor);
            break;
        }
  }

//  const Gdiplus::RectF* ControlPoint::getBoundRect() {
//    if(!boundRect){
//      real adjustVal = 1;
//      boundRect = new Gdiplus::RectF(x()-adjustVal, y()-adjustVal, width()+adjustVal*2, height()+adjustVal*2);
//    }
//    return boundRect;
//  }



  bool ControlPoint::event(const Event* e){
    return Element::event(e);
  }
  void ControlPoint::mouseMoveEvent(const MouseEvent* e){
    //cout<<"ControlPoint.mouseMoveEvent.index is "<<index<<endl;
    if(!e->pressed(MButton_Left)){
      //setMoveCursorStyle();
    }
  }

  void ControlPoint::mouseLeaveEvent(const MouseEvent* e){
    //cout<<"ControlPoint.mouseLeaveEvent.index is "<<index<<endl;
//    WorkPage* page = (WorkPage*)(layer->getDrawPage());
//    WorkLayer* workLayer = page->getWorkLayer();
//    if(page->getWorkMode() == WorkPage::SelectMode){
//     workLayer->resetCursor();
//    }
  }
  void ControlPoint::mousePressEvent(const MouseEvent* e){
    //cout<<"ControlPoint.mousePressEvent.isSelected() is "<<this->isSelected()<<endl;
    //setFocus();
    WorkPage* page = (WorkPage*)(layer->getDrawPage());
    WorkLayer* workLayer = page->getWorkLayer();
    //if(page->getWorkMode() == WorkPage::SelectMode){
    //  workLayer->setCursor(&crossCursor);
    //}
    //setCursor(&crossCursor);
  }

  void ControlPoint::mouseReleaseEvent(const MouseEvent* e){
//    setMoveCursorStyle();
  }

}
